-- round_self_recover3
-- created by wangy
-- 生命值低于xx%时，每回合自动回复N%生命值

return {
    apply = function(prop, baseValue)
        return FormulaM.invoke("CALC_BIG_DIVISION", baseValue, prop[3], 1000);
    end,

    -- 每回合触发
    trigger = function(who, prop, arg)
        local max   = who:getMaxHp();
        local curr  = who:getHp();
        local value = FormulaM.invoke("CALC_BIG_DIVISION", max, tonumber(prop[3]), 1000);

        -- 生命没有降低到界线
        if curr > FormulaM.invoke("CALC_BIG_DIVISION", max, tonumber(prop[2]), 1000) then
            return 0;
        end

        trace("round_self_recover3", "max = %d, curr = %d, value = %d", max, curr, value);

        -- 不能超过上限
        value = math.min(value, max - curr);

        if value <= 0 then
            return 0;
        end

        if who.type == OBJECT_TYPE_USER then
            BonusM.doBonus({ 2, "hp", value, }, "round_self_recover3");
        else
            who:addAttrib("hp", value);
        end

        return value;
    end,

    record = function(target, prop, path)
        return false;
    end
};
